package com.diandiansong.manager.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.diandiansong.app.R;
import com.diandiansong.manager.entity.SalesLoginInfo;
import com.diandiansong.manager.entity.SalesUserInfo;
import com.diandiansong.manager.ui.biz.Achievement;
import com.diandiansong.manager.ui.biz.Customer;
import com.diandiansong.manager.ui.biz.Team;
import com.diandiansong.manager.ui.biz.TeamAchievement;
import com.diandiansong.manager.ui.user.Login;
import com.diandiansong.manager.ui.user.MsgList;
import com.diandiansong.manager.ui.user.SystemSetting;
import com.diandiansong.manager.ui.user.UserInfo;
import com.diandiansong.manager.utils.M;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import diandiansong.library.ui.base.BaseAct;
import diandiansong.library.utils.CCallBack;
import diandiansong.library.utils.ImgLoader;

/**
 * 主页
 */
public class Main extends BaseAct {
    @BindView(R.id.iv_avatar)
    ImageView mIvAvatar;
    @BindView(R.id.tv_shop_name)
    TextView mTvShopName;
    @BindView(R.id.tv_invite_code)
    TextView mTvInviteCode;
    @BindView(R.id.tv_expand_customer)
    TextView mTvExpandCustomer;
    @BindView(R.id.tv_order_customer)
    TextView mTvOrderCustomer;
    @BindView(R.id.tv_total_hard)
    TextView mTvTotalHard;
    @BindView(R.id.go_cus)
    TextView mGoCus;
    @BindView(R.id.go_result)
    TextView mGoResult;
    @BindView(R.id.go_team_result)
    TextView mGoTeamResult;
    @BindView(R.id.go_msg)
    TextView mGoMsg;
    @BindView(R.id.go_sys)
    TextView mGoSys;
    @BindView(R.id.go_team_mana)
    TextView mGoTeamMana;

    public static void start(Context context) {
        Intent starter = new Intent(context, Main.class);
        context.startActivity(starter);
    }

    SalesUserInfo mSalesUserInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        addContentView(R.layout.manager_main);
        hideTitle();

        if (SalesLoginInfo.isManagerOrSalesMan(getActivity()).equals("2")) {
            mGoTeamMana.setVisibility(View.GONE);
            mGoTeamResult.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        M.salesUser(this, new CCallBack<SalesUserInfo>() {
            @Override
            public void onSuccess(SalesUserInfo salesUserInfo) throws Exception {
                mSalesUserInfo = salesUserInfo;
                updateUI();
            }

            @Override
            public void onFailure(int code, String msg) {
                toast(msg);
                if (code == 401) {
                    SalesLoginInfo.clearSalesLoginInfo(getActivity());
                    Login.start(getActivity());
                    finish();
                }
            }
        });
    }

    public void updateUI() {
        SalesUserInfo.Data data = mSalesUserInfo.getData();

        String avatar = data.getAvatar();
        ImgLoader.displayRound(mIvAvatar, avatar);

        setText(mTvShopName, data.getCompany());
        String invitation_code = data.getInvitation_code();
        if (invitation_code != null && !invitation_code.isEmpty()) {
            mTvInviteCode.setVisibility(View.VISIBLE);
            setText(mTvInviteCode, "邀请码：" + invitation_code);

        } else {
            mTvInviteCode.setVisibility(View.GONE);
        }
        setText(mTvExpandCustomer, data.getMonth_member_sum());
        setText(mTvOrderCustomer, data.getMonth_order_sum());
        setText(mTvTotalHard, data.getMonth_total());
    }

    private void goUserInfo() {
        if (mSalesUserInfo == null) {
            M.salesUser(this, new CCallBack<SalesUserInfo>() {
                @Override
                public void onSuccess(SalesUserInfo salesUserInfo) throws Exception {
                    mSalesUserInfo = salesUserInfo;
                    updateUI();
                    UserInfo.start(getActivity(), mSalesUserInfo);
                }

                @Override
                public void onFailure(int code, String msg) {
                    toast("获取资料失败");
                }
            });

        } else {
            UserInfo.start(getActivity(), mSalesUserInfo);
        }
    }

    @OnClick({R.id.iv_avatar, R.id.tv_shop_name, R.id.go_cus, R.id.go_result, R.id.go_team_result, R.id.go_msg, R.id.go_sys, R.id.go_team_mana})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_avatar:
                goUserInfo();
                break;
            case R.id.tv_shop_name:
                goUserInfo();
                break;
            case R.id.go_cus:
                Customer.start(getActivity());
                break;
            case R.id.go_result:
                Achievement.start(getActivity());
                break;
            case R.id.go_team_result:
                TeamAchievement.start(getActivity());
                break;
            case R.id.go_msg:
                MsgList.start(getActivity());
                break;
            case R.id.go_sys:
                SystemSetting.start(getActivity());
                break;
            case R.id.go_team_mana:
                Team.start(getActivity());
                break;
        }
    }

    public static class Finish {
    }

    public static void doFinish() {
        EventBus.getDefault().post(new Finish());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveFinish(Finish finish) {
        finish();
    }
}